using System;
using System.Text.RegularExpressions;

namespace WebAndLoadTestPluginsAndRules.Common
{
    public class RegexFilter : Filter
    {
        public string Filter { get; set; }
        public bool IgnoreCase { get; set; }
        private Regex _regex;
        private Regex Regex
        {
             get
            {
                if (_regex == null)
                {
                    _regex = new Regex(Filter, IgnoreCase ? RegexOptions.IgnoreCase : RegexOptions.None);
                }
                return _regex;
            }
        }

        public RegexFilter(string filter, bool ignoreCase)
        {
            if (string.IsNullOrEmpty(filter))
            {
                throw new ArgumentException("filter must be specified", "filter");
            }
            Filter = filter;
            IgnoreCase = ignoreCase;
        }


        public override bool Matches(string value)
        {
            if (value == null)
            {
                return false;
            }
            return Regex.IsMatch(value);
        }
    }
}